package com.fuxiang.mall.order.feign;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fuxiang.mall.order.domain.Product;

import feign.hystrix.FallbackFactory;

/**
 * @description: Product服务调用
 * @author: zhongyy
 * @date: 2020-04-16 19:19
 **/
@FeignClient(name = "mall-product", fallbackFactory = FeignClientFallbackFactory.class)
public interface ProductFeignClient {

    /**获取产品信息
     * @param id 产品ID
     * @return 产品信息
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    Product findById(@PathVariable("id") Long id);
}

/**
 * UserFeignClient的fallbackFactory类，该类需实现FallbackFactory接口，并覆写create方法
 */
@Component
class FeignClientFallbackFactory implements FallbackFactory<ProductFeignClient> {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

    @Override
    public ProductFeignClient create(Throwable cause) {
        return id -> {
            FeignClientFallbackFactory.LOGGER.info("fallback; reason was:", cause);
            Product product = new Product();
            product.setId(-1L);
            return product;
        };
    }
}